package com.yupi.springbootinit.judge.codesanbox.model.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.judge.codesanbox.CodeSanBox;
import com.yupi.springbootinit.judge.codesanbox.model.ExecuteCondeRequest;
import com.yupi.springbootinit.judge.codesanbox.model.ExecuteCondeResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * 远程代码沙箱（实际调用接口的沙箱）
 */
public class RemoteCodeSanBox implements CodeSanBox {
    @Override
    public ExecuteCondeResponse executeCode(ExecuteCondeRequest executeCondeRequest) {
        System.out.println("远程代码沙箱");
        String url = "http://localhost:8080/executeCode";
        String json = JSONUtil.toJsonStr(executeCondeRequest);
        String responseStr = HttpUtil.createPost(url)
                .body(json)
                .execute()
                .body();
        if (StringUtils.isBlank(responseStr)) {
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR,"executeCode remoteSandbox error message =" + responseStr);
        }
        return JSONUtil.toBean(responseStr,ExecuteCondeResponse.class);
    }
}
